package io.gitee.twoke.generator.spring.boot;

import io.gitee.twoke.generator.spring.SpringGenerateEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;

@Configuration
@AutoConfigureAfter({DataSourceAutoConfiguration.class, TaskExecutionAutoConfiguration.class})
@EnableConfigurationProperties(GeneratorProperties.class)
@ConditionalOnProperty(prefix = "spring.generator", name = "enable", havingValue = "true")
public class GenerateEngineAutoConfiguration extends AbstractGenerateEngineAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public SpringGenerateEngineConfiguration springProcessEngineConfiguration(
            DataSource dataSource,
            PlatformTransactionManager transactionManager,
            GeneratorProperties generatorProperties) throws IOException {
        SpringGenerateEngineConfiguration conf = new SpringGenerateEngineConfiguration();
        conf.setDataSource(dataSource);
        conf.setTransactionManager(transactionManager);
        conf.setDatabaseSchema(generatorProperties.getDatabaseSchema());
        conf.setDatabaseSchemaUpdate(generatorProperties.getDatabaseSchemaUpdate());
        return conf;
    }

}